<?php
namespace App\Http\Controllers\ApiApp\Stock;

use App\DBs\Stock\BoardDB;
use Illuminate\Http\Request;

class BoardController extends BaseController
{
    /**
     * 库
     */

    protected $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = new BoardDB();
    }

    /**
     * 列表
     */
    public function getIndex(Request $request)
    {
        //接口权限值
        $access = $request->post('appAccess','');
        if (env('APP_ENV')!='dev' &&
            $access!=$this->db->getAccessLast()) {
            return $this->toAppJson(-1);
        }
        $page = $request->post('page',0);
        $size = $request->post('size',$this->limit);
        $pageStart = $page * $size;
        $uid = $request->post('uid',0);
        $bid = $request->post('bid',0);
        if (!$uid || !$bid) {
            return $this->toAppJson(-2);
        }
        $resDb = $this->db->getAppIndex($pageStart, $size, $uid, $bid);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-5,array(),$resDb['msg']);
        }
        $dataArr = $resDb['data'];
        $total = $resDb['total'];
        return $this->toAppJson(0, $dataArr ,'获取成功', $total);
    }

    /**
     * 增加，创板记录、素材忽略
     */
    public function setAdd(Request $request)
    {
        //接口权限值
        $access = $request->post('appAccess','');
        if (env('APP_ENV')!='dev' &&
            $access!=$this->db->getAccessLast()) {
            return $this->toAppJson(-1);
        }
        $uid = $request->post('uid',0);
        $plusId = $request->post('plusId',0); //创单ID
        if (!$uid || !$plusId) {
            return $this->toAppJson(-2);
        }
        //获取用户信息
        $resUser = $this->db->getUnameByUid($uid);
        //看看有没有，有的话，直接返回
        $exists = $this->db->getExistByPlus($uid,$plusId);
        $isExist = $exists['isExist'];
        $stockOne = $exists['stockOne'];
        if ($isExist==1) {
            $dataArr = $this->getSourceList($exists);
            return $this->toAppJson(0, $dataArr, '数据已存在');
        }
        //没有则重新添加
        $vid = $stockOne ? $stockOne['id'] : 0;
        $dataArr = array(
            'uid' => $uid,
            'uname' => $resUser,
            'vid' => $vid,
            'plus' => $plusId,
            'isRender' => 1,
            'created_at' => time(),
        );
        $resDb = $this->db->setAppAdd($exists, $dataArr);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-6,array(),$resDb['msg']);
        }
        //返回的素材信息
        $resExists = $this->db->getExistByPlus($uid,$plusId);
        $dataArr = $this->getSourceList($resExists);
        return $this->toAppJson(0, $dataArr, '新增成功');
    }

    /**
     * 需要返回的信息
     */
    public function getSourceList($exists)
    {
        $isExist = $exists['isExist'];
        $stockOne = $exists['stockOne'];
        $plusOne = $exists['plusOne'];
        $boardOne = $exists['boardOne'];
        $sources = $exists['sources'];
        $sourceList = array();
        if (count($sources)) {
            foreach ($sources as $source) {
                $link = $source['type']==1 ? env('DOMAIN').$source['link'] : $source['link'];
                $sourceList[] = [
                    'serial' => $source['serial'],
                    'type' => $source['type'],
                    'link' => $link,
                ];
            }
        }
        return array(
            //库信息
            'vid' => $stockOne['id'],
            'vTitle' => $stockOne['fileTitle'],
            'vType' => $stockOne['fromType'],
            'vImg' => $stockOne['fileImg'],
            'vRatio' => 1280 / 720,
            'vLink' => $stockOne['fileLink'],
            //创单信息
            'plusId' => $plusOne['id'],
            //创板信息
            'uid' => $boardOne['uid'],
            'uname' => $boardOne['uname'],
            'boardId' => $boardOne['id'],
            'isRender' => $boardOne['isRender'],
//            'boardHtml' => $stockOne['boardHtml'],
            //素材信息
            'sourceList' => $sourceList,
        );
    }
}